let funcretdata = {
    isCall: false,
    succ:function(){},
    fail:function(){}
}
const _loginOut = Symbol('_loginOut')
export default class Controller {
    static fnArr = [
        { fnName: 'sendTalkMsg', params: 'talk_id,content,type', tip: '发送会话消息' },
        { fnName: 'makeDialogue', params: 'title,toMemberId,content', tip: '创建临时会话' },
        { fnName: 'editSelfName', params: 'name', tip: '修改用户姓名' },
        { fnName: 'editAvatarurl', params: 'file', tip: '修改用户头像' },
        { fnName: 'addFriendUseMemberId', params: 'member_id', tip: '通过MemberId添加好友 ' },
        { fnName: 'addFriendUseTalkId', params: 'talk_id', tip: '通过TalkId添加好友' },
        { fnName: 'examineToFriend', params: 'clientMsgId,userSelect', tip: '审核好友' },
        { fnName: 'deleteFriend', params: 'talk_id', tip: '删除好友' },
        { fnName: 'findFriendFromTel', params: 'tel', tip: '通过手机号搜索用户' },
        { fnName: 'addTalkObjectCallback', params: 'onNewChatAlert,onListRefresh,onTalkDetailRefresh,onTalkWithMsg', tip: 'talkObject 添加回调函数。' },
        { fnName: 'getTalkList', params: '', tip: "获取会话列表" },
        { fnName: 'getChat', params: 'talk_id', tip: "获取历史记录" },
        { fnName: 'fetchFriendApplyList', params: '', tip: "获取申请列表数据" },
        { fnName: 'resend', params: '', tip: "消息重发" },
        { fnName: 'getTalkDetail', params: 'talk_id', tip: "取会话用户信息" },
        { fnName: 'exitTalk', params: '', tip: "用户退出聊天界面" },
        { fnName: 'setConfig', params: 'option', tip: "socket实例" },
        { fnName: 'loginOut', params: '', tip: "(测试)断开连接" },
        { fnName: 'quickConnect', params: 'key,token,member_id,port', tip: "快速登录" },
        { fnName: 'readyStateLogin', params: '', tip: "判断登录状态" },
      ]
    static funcretdata={
        isCall: false,
        succ:function(){},
        fail:function(){}
    }
    #socket = {}
    constructor(socket) {
        this.funcretdata={
            isCall: false,
            succ:function(){},
            fail:function(){}
        }
        this.#socket = socket;
        this.api = this.#socket.api
        
    }
    /* 初始化属性 */
    setProperty(member_id, token, key, h_url, a_url, e_url) {
        this.#socket.api.member_id = member_id;
        this.#socket.api.token = token;
        this.#socket.api.key = key;
        this.#socket.api.h_url = h_url;
        this.#socket.api.a_url = a_url;
        this.#socket.api.e_url = e_url;
        return
    }
    action(fnName, params, succCallback=null, failCallback=null) {
        // 验证方法名
        let fn = null
        Controller.fnArr.find(item => { if(item.fnName == fnName){fn = item} })
        if (!fn) { return failCallback({ code: 301, data: `${fnName}方法不存在！` }) }
        //  解析需传参数
        let paramsType = (fn.params instanceof Array);
        (!paramsType && fn.params !=='') ? fn.params = fn.params.split(',') : null;
        (!paramsType && fn.params =='') ? fn.params = [] : null;
        const paramsList = [];
        //验证参数
        let loseKey = null
        loseKey = fn.params.find((item) => { if (!params[item] && !params.noMust) { loseKey=item } paramsList.push(params[item])  })
        if (loseKey) {
            const data = { code: 301, data: fnName + '函数缺少' + loseKey + '参数!' };
            failCallback && failCallback(data);
        }

        funcretdata.succ = succCallback
        funcretdata.fail = failCallback
        funcretdata.isCall = false
        this.#socket.api.paramsObj = params
        this.api[fnName](...paramsList, succCallback, failCallback);
        return
    }
    onSucc(data){
        Controller.funcretdata = funcretdata
        if(Controller.funcretdata.isCall == true){
            console.log('程序出错，回调函数已经被调用过')
        }
        Controller.funcretdata.isCall = true
        Controller.funcretdata.succ(data)
        return
    }
    onFail(data){
        if(Controller.funcretdata.isCall == true){
            console.log('程序出错，回调函数已经被调用过')
        }
        Controller.funcretdata.isCall = true 
        Controller.funcretdata.fail(data)
        return
    }
    /* 清理属性 */
    clear(){
        this.member_id = ''
        this.token = ''
        this.key = ''
        this.h_url = ''
        this.a_url = ''
        this.e_url = ''
        return
    }
    /*(测试)断开连接*/
    [_loginOut]() {
        this.#socket.so.close();
        this.#socket.so = null;
        return
    }

}